package widget

import (
	"image/color"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/theme"
	wit "fyne.io/fyne/v2/widget"
	"gitee.com/hanholy1210/fyne_holy/container"
)

type Entry struct {
	wit.BaseWidget

	Title string
	Value string

	titleWidth float32
	valueWidth float32
	titleSize  float32
	titleColor color.Color
	titleStyle fyne.TextStyle

	titleCanvas *canvas.Text
	valueEntry  *wit.Entry
	titleLay    *fyne.Container
	valueLay    *fyne.Container
}

func NewEntry() *Entry {
	e := &Entry{
		Title:      "",
		Value:      "",
		titleColor: theme.Color(theme.ColorNameForeground),
		titleStyle: fyne.TextStyle{},
	}
	e.ExtendBaseWidget(e)
	return e
}
func NewEntryWithTitle(t string) *Entry {
	e := &Entry{
		Title:      t,
		Value:      "",
		titleColor: theme.Color(theme.ColorNameForeground),
		titleStyle: fyne.TextStyle{},
	}
	e.ExtendBaseWidget(e)
	return e
}
func (e *Entry) CreateRenderer() fyne.WidgetRenderer {
	e.titleCanvas = canvas.NewText(e.Title, e.titleColor)
	e.valueEntry = wit.NewEntry()
	if e.titleSize > 0 {
		e.titleCanvas.TextSize = e.titleSize
	}
	e.titleCanvas.TextStyle = e.titleStyle
	if e.titleWidth != 0 {
		e.titleLay = container.NewSize(
			fyne.NewSize(e.titleWidth, e.titleCanvas.TextSize),
			e.titleCanvas)
	} else {
		e.titleLay = container.NewCenter(e.titleCanvas)
	}
	if e.valueWidth != 0 {
		e.valueLay = container.NewSizeMax(
			fyne.NewSize(e.valueWidth, e.valueEntry.MinSize().Height),
			e.valueEntry)
	} else {
		e.valueLay = container.NewCenter(e.valueEntry)
	}

	lay := container.NewHBoxNoPadding(e.titleLay, e.valueLay)
	return wit.NewSimpleRenderer(lay)
}
func (e *Entry) SetTitleWidth(t float32) *Entry {
	e.titleWidth = t
	return e
}
func (e *Entry) SetValueWidth(v float32) *Entry {
	e.valueWidth = v
	return e
}
func (e *Entry) SetWidth(t, v float32) *Entry {
	e.titleWidth = t
	e.valueWidth = v
	return e
}

func (e *Entry) SetTitleSize(t float32) *Entry {
	e.titleSize = t
	return e
}
func (e *Entry) SetTitleColor(t color.Color) *Entry {
	e.titleColor = t
	return e
}
func (e *Entry) SetTitleStyle(s fyne.TextStyle) *Entry {
	e.titleStyle = s
	return e
}
func (e *Entry) UpdateTitle(v string) *Entry {
	e.Title = v
	e.titleCanvas.Text = v
	e.Refresh()
	return e
}
func (e *Entry) UpdateValue(v string) *Entry {
	e.Value = v
	e.valueEntry.SetText(v)
	e.Refresh()
	return e
}
